package MTT::Libs::Report::Writer;

sub new {
	my $class = shift;
	my $self = ref $_[0] eq 'HASH' ? $_[0] : { @_ };
	$self->{row} = 0;
	$self->{count} = 0;
	return bless $self, $class;
}

sub init {}

sub add_page { 
	my ($self, $name, $header) = @_;

	$self->{row} = 0;

	$self->_add_page($name, $header);
	$self->add_row($header);
}

sub _add_page {}

sub add_row {
	my ($self, $row) = @_;

	$self->{count}++;
	$self->_add_row( $row );
	$self->{row}++;
}

sub _add_row {};

sub end_page {}
sub finish {}

sub result { return shift->{result} }

sub rows { return shift->{count} };



package MTT::Libs::Report::Writer::HTML;

our @ISA = qw(MTT::Libs::Report::Writer);

sub _add_page {
	my $self = shift;
	my (undef, $header) = @_;
	
	$self->{res} = [];
}

sub _add_row { 
	my ($self, $row) = @_;

	push @{ $self->{res} }, [ @$row ];
}

sub end_page {
	my ($self, $header) = @_;
	if ( @{ $self->{res} } ) {
		push @{ $self->{result} }, @{ $self->{res} };
	}
}

# ----------------------------
# Excel
#-----------------------------
package MTT::Libs::Report::Writer::Excel;

our @ISA = qw(MTT::Libs::Report::Writer);

use Data::Dumper;

sub init {
	my ($self) = @_;
	$self->{workbook}  = Spreadsheet::WriteExcel->new( $self->{file} );

	if ( $self->{rowspan} && ! $self->{merge_format} ) {
		$self->{merge_format} = $self->{workbook}->add_format(
			align => 'center',
			valign => 'vcenter'
		);
	}
}

sub _add_page {
	my ($self, $name, $header) = @_;
	$self->{worksheet} = $self->{workbook}->add_worksheet($name);
	$self->{row} = 0;
	$self->{column} = 0;
}

sub _add_row {
	my ($self, $data) = @_;

	$self->{column} = 0;
	
	if ( scalar( @$data ) == 13 ) {
		$data->[11] =~ s/^0$//;
	}
	else {
		$data->[10] =~ s/^0$//;
	}

	my $merge = 0;
	if ( $self->{rowspan} > 1 && $self->{row} > 1 
		&& $self->{row} % $self->{rowspan} == 0 ) 
	{
		$merge = 1;
	}

	foreach ( @$data ) {
		$self->{worksheet}->write( $self->{row}, $self->{column}, $_ );

		if ( $merge ) {
			$self->{worksheet}->merge_range(
				$self->{row} - $self->{rowspan} + 1,
				$self->{column},
				$self->{row},
				$self->{column},
				$_,
				$self->{merge_format}
			) if $self->{column} <= $self->{colspan};
		}

		$self->{column}++;
	}

#	$self->{row}++;
}


sub end_page {
	my ($self) = @_;
}

sub finish {
	my ($self) = @_;

	my $format = $self->{workbook}->add_format(
		bold => 1,
		color => 'yellow',
		bg_color => 'gray',
		valign => 'vcenter',
		border => 1,
	);
	$self->{worksheet}->set_row(0, 23, $format, 0, 0, 1);
	$self->{workbook}->close();
}

package MTT::Libs::Report::Writer::CSV;

our @ISA = qw(MTT::Libs::Report::Writer);

use IO::File;

sub init {
	my ($self) = @_;
	my $fh = IO::File->new( $self->{file}, 'w' ) || die $!;
	$fh->binmode(':utf8');
	$self->{fh} = $fh;
}

#sub add_page {
#	my ($self, undef, $header) = @_;
#	if ($header) {
#		$self->add_row( $header );
#	}
#}

sub _add_row {
	my ($self, $row) = @_;
	my $fh = $self->{fh};
	print $fh join(",", map { qq{"$_"} } @$row), "\n";
}

sub finish {
	my ($self) = @_;
	$self->{fh}->close();
}

package MTT::Libs::Report::Writer::PDF;

our @ISA = qw(MTT::Libs::Report::Writer);

use PDF::Reuse;

sub init {
	my ($self) = @_;
	prFile( $self->{file} );
    prFontSize(10);

	$self->{Y} = 550;
	$self->{step} = 13;
}

sub add_page {
	my ($self, undef, $header) = @_;
	prPage();
	if ($header) {
		$self->add_row( $header );
	}
}

sub _add_row {
	my ($self, $row) = @_;
	if ( $self->{Y} - $self->{step} < 13 ) {
		$self->add_page();
	}

	prText( 13, $self->{Y}, join("\t", @$row) ); 
	$self->{Y} -= $self->{step};
}

sub finish {
	my ($self) = @_;
	prEnd();
}

1;

